<template>
    <div class="app-container">
        <div class="filter-container">
            <el-form :model="queryParams"  :inline="true">

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table  v-loading="loading" :data="dataList"  fit  highlight-current-row style="width: 100%;">
 
            <el-table-column prop="tableName" align="center" label="表名"  />
            <el-table-column prop="author" align="center" label="作者名"  />
            <el-table-column prop="category" :formatter="categoryDictFormat" align="center" label="分类"  />
            <el-table-column prop="packageName" align="center" label="生成包路径"  />
            <el-table-column prop="moduleName" align="center" label="生成模块名"  />
            <el-table-column prop="className" align="center" label="实体名"  />
            <el-table-column label="更新时间" align="center" prop="updateDate">
                <template slot-scope="scope">
                    <span>{{ scope.row.updateDate | dateFormat('YYYY-MM-DD HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" >
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            @click="handleGen(scope.row)"
                    >生成代码</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination  :total="total" :page.sync="queryParams.current"  :limit.sync="queryParams.size" @pagination="getList"  />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="数据源" prop="sourceId">
                            <!-- <el-input v-model="form.sourceId" placeholder="请输入数据源id" /> -->
                             <el-select v-model="form.sourceId" placeholder="请选择">
                                <el-option
                                v-for="item in sourceIds"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="作者名" prop="author">
                            <el-input v-model="form.author" placeholder="请输入作者名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="表名" prop="tableName">
                            <el-input v-model="form.tableName" placeholder="请输入表名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="分类" prop="category">
                            <!-- <el-input v-model="form.category" placeholder="请输入分类" /> -->
                            <el-select v-model="form.category" placeholder="请选择">
                                <el-option
                                v-for="item in categoryDict"
                                :key="item.value"
                                :label="item.name"
                                :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="包路径" prop="packageName">
                            <el-input v-model="form.packageName" placeholder="请输入生成包路径" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="模块名" prop="moduleName">
                            <el-input v-model="form.moduleName" placeholder="请输入生成模块名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="实体名" prop="className">
                            <el-input v-model="form.className" placeholder="请输入实体名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                         <el-form-item label="备注">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>


<script>
    import Pagination from '@/components/Pagination'
    export default {
        name: 'GenScheme',
        components: { Pagination },
        data() {
            return {
                // 弹出层标题
                title: '',
                // 遮罩层
                loading: true,
                // 总数量
                total: 0,
                // 列表数据
                dataList: [],
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    current: 1,
                    size: 10,
                },
                sourceIds: [],
                // 分类数据字典
                categoryDict: [{
                    value: '0',
                    name: '普通'
                },
                {
                    value: '1',
                    name: '树形'
                }, {
                    value: '2',
                    name: '自定义'
                }],
                // 表单参数
                form: {

                },
                // 表单校验
                rules: {
                    sourceId: [
                        { required: true, message: '数据源不能为空', trigger: 'blur' }
                    ],
                    category: [
                        { required: true, message: '分类不能为空', trigger: 'blur' }
                    ],
                    author: [
                        { required: true, message: '作者名称不能为空', trigger: 'blur' }
                    ],
                    tableName: [
                        { required: true, message: '表名称不能为空', trigger: 'blur' }
                    ],
                    packageName: [
                        { required: true, message: '包路径不能为空', trigger: 'blur' }
                    ],
                    moduleName: [
                        { required: true, message: '模块名称不能为空', trigger: 'blur' }
                    ]
                
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
              // 字典翻译
            categoryDictFormat(row, column) {
                const obj = this.categoryDict.find(dict => dict.value == row.category)
                return obj.name
            },
            /** 查询列表 */
            getList() {
                this.$store.dispatch('generator/genScheme/findPage', this.queryParams)
                    .then((data) => {
                    this.dataList = data.records
                    this.total = data.total
                    this.loading = false
                })
            .catch((err) => {
                    console.log('失败' + err)
                })
            },
            // 取消按钮
            cancel() {
                this.open = false
                this.reset()
            },
            // 表单重置
            reset() {
                this.form = {
                    sourceId: '0',
                    tableName: undefined,
                    author: undefined,
                    packageName: undefined,
                    moduleName: undefined,
                    className: undefined,
                    remark: undefined,
                    category: '0'
                }
                this.getDataSourceAll()
                this.$nextTick(()=>{
                    this.$refs['form'].resetFields();
                 })
            },
            getDataSourceAll() {
                this.$store.dispatch('generator/genScheme/getDataSourceAll')
                .then(data => {
                    this.sourceIds = data
                    this.sourceIds.unshift({id:'0', name:'默认数据源'})
                
                }).catch((err) => {
                    console.log('失败' + err)
                })
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset()
                this.open = true
                this.title = '添加'
            },
            /** 删除按钮操作 */
            handleDel(id) {
                this.$store.dispatch('generator/genScheme/delById', id).then(() => {
                    this.getList()
                    this.msg('删除成功')
                })
            },
            handleGen (row) {
                this.$confirm('是否确认生成名称为"' + row.tableName + '"的数据项?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                   window.location.href = process.env.VUE_APP_BASE_API + '/generator/genscheme/generator/'+row.id; 
                })
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset()
                this.$store.dispatch('generator/genScheme/getById', row.id)
                    .then((data) => {
                    this.form = data
                    this.open = true
                    this.title = '修改'
                }).catch((err) => {
                    console.log('失败' + err)
                })
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.id !== undefined) {
                            this.$store.dispatch('generator/genScheme/updateById', this.form).then(data => {
                                this.msg('修改成功')
                                this.open = false
                                this.getList()
                            })
                        } else {
                            this.$store.dispatch('generator/genScheme/saveData', this.form).then(res => {
                                this.msg('新增成功')
                                this.open = false
                                this.getList()
                            })
                        }
                    }
                })
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                this.$confirm('是否确认删除名称为"' + row.tableName + '"的数据项?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleDel(row.id)
                })
            }
        }
    }
</script>
